package com.cyl.sms.service;

import com.ruoyi.common.config.properties.SmsProperties;
import com.ruoyi.common.core.domain.model.SmsResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.sms.AliyunSmsTemplate;
import com.ruoyi.common.core.sms.SmsTemplate;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.SmsUtils;
import com.ruoyi.system.service.ISysConfigService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SmsService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SmsProperties smsProperties;

    @Autowired
    private ISysConfigService configService;
    

    public boolean checkCode(String phones,String uuid,String code){
    	
    	
    	if(phones==null) {
            throw new RuntimeException("当前登录人员，手机号未设置！");
    	}
    	if(uuid==null) {
            throw new RuntimeException("验证码唯一编号必填！");
    	}
    	if(code==null) {
            throw new RuntimeException("请输入验证码！");
    	}
   	 	String  SMS_CHECK_DOUBLE = configService.selectConfigByKey("SMS_CHECK_DOUBLE");
   	    if(SMS_CHECK_DOUBLE!=null && "true".equals(SMS_CHECK_DOUBLE)) {
   	    	return checkCode5Minutes(phones, code);
   	    }	
    	String dbCode = redisCache.getCacheObject(uuid + "_" + phones);
    	if(dbCode!=null && dbCode.equals(code)) {
        	redisCache.deleteObject(uuid + "_" + phones);
    		return true;
    	}else {
            throw new RuntimeException("验证码不正确，或过期！");
    	}
    }
    
    private boolean checkCode5Minutes(String phones,String code) {
    	String dbCode = redisCache.getCacheObject(phones);
    	if(dbCode!=null && dbCode.equals(code)) {
    		return true;
    	}else {
            throw new RuntimeException("验证码不正确，或过期！");
    	}
    }
    	
    public ResponseEntity<SmsResult> sendAliyun(String phones){
     
        //byte[] decodedBytes = Base64.getDecoder().decode(phones);
        //phones = new String(decodedBytes);
        String code = SmsUtils.createRandom(true, 4);
        Map<String, String> map = new HashMap<>();
        map.put(smsProperties.getYzmTemplateCs(), code);
        SmsTemplate smsTemplate = new AliyunSmsTemplate(smsProperties);
        SmsResult send =  smsTemplate.send(phones, smsProperties.getYzmTemplateId() ,map);
        // 验证码存redis，当前只用于注册和登录，手机号只有一个
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        send.setUuid(uuid);
        try{
            String  SMS_CHECK_DOUBLE = configService.selectConfigByKey("SMS_CHECK_DOUBLE");
       	    if(SMS_CHECK_DOUBLE!=null && "true".equals(SMS_CHECK_DOUBLE)) {
                redisCache.setCacheObject(phones, code, 5, TimeUnit.MINUTES);
       	    }else {
                redisCache.setCacheObject(uuid + "_" + phones, code, 5, TimeUnit.MINUTES);
       	    }
        }catch (Exception e){ 
            return ResponseEntity.ok(
                    SmsResult.builder()
                            .isSuccess(false)
                            .message("服务繁忙，请稍后再试")
                            .build()
            );
        }
        return ResponseEntity.ok(send);
    }
}
